/**
 * 
 */
package org.h2.command.ddl;

import java.sql.SQLException;
import java.util.Hashtable;

import org.h2.command.Prepared;
import org.h2.engine.Database;
import org.h2.engine.Session;
import org.h2.message.Message;
import org.h2.schema.Schema;
import org.h2.table.Column;
import org.h2.table.TableExtern;
import org.h2.util.ObjectArray;

/**
 * @author falario
 *
 */
public class CreateExternTable extends SchemaCommand {
	
	private String tableName;
    private boolean ifNotExists;
    private String comment;
    
    private ObjectArray columns=new ObjectArray();
    private ObjectArray constraintCommands=new ObjectArray();
    private String pkColumnNames[];
    private boolean hashPrimaryKey;
    private boolean onCommitDrop;
    private boolean onCommitTruncate;
    private boolean persistent;
    
    private String type=null;
    private Hashtable parameters=new Hashtable();

	/**
	 * @param session
	 * @param schema
	 */
	public CreateExternTable(Session session, Schema schema, boolean persistent) {
		super(session, schema);
		// TODO Auto-generated constructor stub
		this.persistent=persistent;
	}

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public void setIfNotExists(boolean ifNotExists) {
        this.ifNotExists = ifNotExists;
    }

    public int update() throws SQLException {
        session.commit();
        Database db = session.getDatabase();
        session.getUser().checkAdmin();
        if(getSchema().findTableOrView(session, tableName)!=null) {
            if (ifNotExists) {
                return 0;
            }
            throw Message.getSQLException(Message.TABLE_OR_VIEW_ALREADY_EXISTS_1,
                    tableName);
        }
        int id = getObjectId(false, true);
        
        Column[] cols=new Column[this.columns.size()];
        if(cols.length==0){
        	cols=null;
        }
        else{
        	this.columns.toArray(cols);
        }
        TableExtern table = new TableExtern(getSchema(), id, tableName, cols, pkColumnNames, persistent, type, parameters);
        this.columns=new ObjectArray(table.getDelegated().getColumns());
        
        //TableLink table = new TableLink(getSchema(), id, tableName, driver, url, user, password, originalTable);
        
        table.setComment(comment);
        db.addSchemaObject(session, table);
        
        return 0;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }
    
    public void addConstraintCommand(Prepared command) throws SQLException {
        if(command instanceof CreateIndex) {
            CreateIndex create = (CreateIndex) command;
            if(create.getPrimaryKey()) {
                setPrimaryKeyColumnNames(create.getColumnNames());
                setHashPrimaryKey(create.getHash());
            } else {
                constraintCommands.add(command);
            }
        } else {
            constraintCommands.add(command);
        }
    }
    
    public void setPrimaryKeyColumnNames(String[] colNames) throws SQLException {
        if(pkColumnNames != null) {
            if(colNames.length != pkColumnNames.length) {
                throw Message.getSQLException(Message.SECOND_PRIMARY_KEY);
            }
            for(int i=0; i<colNames.length; i++) {
                if(!colNames[i].equals(pkColumnNames[i])) {
                    throw Message.getSQLException(Message.SECOND_PRIMARY_KEY);
                }
            }
        }
        this.pkColumnNames = colNames;
    }
    
    public void addColumn(Column column) {
        columns.add(column);
    }
    
    public void setHashPrimaryKey(boolean b) {
        this.hashPrimaryKey = b;
    }
    
    public void setOnCommitDrop() {
        this.onCommitDrop = true;
    }

    public void setOnCommitTruncate() {
        this.onCommitTruncate = true;
    }
    
    public void addParameter(String key, String value){
    	this.parameters.put(key, value);
    }
    
    public void setType(String type){
    	this.type=type;
    }
}